package com.pvdnc.tapkit.chat.chat.server;

import com.pvdnc.nsocket.server.ClientManager;
import com.pvdnc.nsocket.server.handler.SimpleServerHandler;
import com.pvdnc.tapkit.chat.chat.ChatServer;
import com.pvdnc.tapkit.chat.chat.client.ClientInfo;
import com.pvdnc.tapkit.chat.chat.packet.ClientIdle;
import com.pvdnc.tapkit.chat.chat.packet.GetFriendRequest;
import com.pvdnc.tapkit.chat.chat.packet.GetFriendResponse;

import java.util.List;

import io.netty.channel.ChannelHandlerContext;

public class OnFriendRequest extends SimpleServerHandler<ChatServer, GetFriendRequest> {
    public OnFriendRequest(ChatServer server) {
        super(server, GetFriendRequest.COMMAND);
    }

    @Override
    public void onReceived(ChannelHandlerContext ctx, GetFriendRequest msg) {
        GetFriendResponse response=new GetFriendResponse();
        List<String> clientNameList = ClientManager.getInstance().listClient();
        for (String name:clientNameList){
            ClientInfo clientInfo=new ClientInfo();
            clientInfo.mName=name;
            clientInfo.mPublicKey=getServer().mRSAKeyManager.getKey(name);

            response.mClientInfoMap.put(name,clientInfo);
        }
        ctx.channel().writeAndFlush(response);
    }
}
